package com.youkeda.yunmusic.web.control;

import com.youkeda.yunmusic.web.model.Comment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;
@Controller
public class CommentPostControl {
    private static Map<String , List<Comment>> comments=new HashMap<>();
    //用于对评论进行排序按从晚到早
    @RequestMapping(value = "/comment/post")
    public String post(@RequestParam(value = "songId") String songId, Comment comment){
        System.out.println("运行CommentPostControl中的post函数");
        //实例化一个date对象
        Date commentTime=new Date();
        comment.setCommentTime(commentTime);
        comment.setId(UUID.randomUUID().toString());
        comments.computeIfAbsent(songId,k->new ArrayList<>()).add(comment);
        List<Comment>commentList=comments.get(songId);
        Collections.sort(commentList,  (o1,o2)->{
            if (o1.getCommentTime().after(o2.getCommentTime())){
                return -1;
            } else {
                return 1;
            }
        });

        return "redirect:/comments";
    }
    public List<Comment>getComments(String songId){

        return comments.get(songId);
    }
}
